package cxy.highConcurrency;

public class RaceConditionDemo {
	private volatile long count = 0;

	synchronized long get(){
		return count;
	}

	synchronized void set(long v) {
		count = v;
	}

    void add10K() { 
		int idx = 0; 
		while(idx++ < 10000) {
			set(get()+1) ;
		} 
	}
	
	public static void main(String[] args) throws InterruptedException {
		RaceConditionDemo raceConditionDemo = new RaceConditionDemo();
		
		Thread t1 = new Thread (() -> {
			raceConditionDemo.add10K();
			System.out.println(raceConditionDemo.get());
		});
		
		Thread t2 = new Thread (() -> {
			raceConditionDemo.add10K();
			System.out.println(raceConditionDemo.get());
		});
		
		t1.start();
		t2.start();
		
		Thread.currentThread().sleep(1000);
		
		System.out.println(raceConditionDemo.get());
	}
}
